package com.jewaton.auth.commons.util;

import java.util.Collections;
import java.util.List;



/**   
*    
* 分页工具类
*
*/ 
public class Pager<T> implements java.io.Serializable {

	private String url;
	private int pageNo = 1; // 当前页
	private int pageSize = 20; // 每页记录数
	private int totalRecord = 0;// 总记录数
	private int totalPage = 1;// 总页数
	private boolean prev = false;// 是否有上一页
	private boolean next = false;// 是否有下一页
	
	private List<T> list = Collections.emptyList();// 区间集合
	
	public Pager(){}


	public Pager(int pageNo, int pageSize, int totalRecord){
		this.pageNo = pageNo > 0 ? pageNo : 1;
		this.pageSize = pageSize > 0 ? pageSize : 10;
		this.totalRecord = totalRecord > 0 ? totalRecord : 0;
		this.totalPage = (totalRecord + pageSize - 1) / pageSize;
		this.prev = pageNo > 1;
		this.next = pageNo < this.totalPage;
	}

	
	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(int totalRecord) { 
		this.totalRecord = totalRecord > 0 ? totalRecord : 0;
		this.totalPage = (totalRecord + pageSize - 1) / pageSize;
		this.prev = pageNo > 1;
		this.next = pageNo < this.totalPage;
	}

	public boolean isPrev() {
		return prev;
	}

	public void setPrev(boolean prev) {
		this.prev = prev;
	}

	public boolean isNext() {
		return next;
	}

	public void setNext(boolean next) {
		this.next = next;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public int getStart() {
		return (getPageNo() - 1) * getPageSize();
	}

	public int getEnd() {
		return getStart() + getPageSize();
	}
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getNextPage() {
		return Math.min(pageNo+1,totalPage);
	}
	public int getPreviousPage() {
		return Math.max(pageNo-1,1);
	}
	
}
